package de.karlNet.login;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Controller;


@Controller
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class LoginBean {
	@Autowired
	private LoginCommand loginCommand;
	@Autowired
	private LdapUsernameLookup ldapUsernameLookup;

	private String password;
	private String username;
	private boolean failed = false;
	private boolean loggedIn= false;

	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
	}

	public boolean isFailed() {
		return failed;
	}

	public void setFailed(boolean failed) {
		this.failed = failed;
	}

	public String getPassword() {
		return password;
	}

	public String getUsername() {
		return username;
	}

	public String login() {
		String dn = this.ldapUsernameLookup.lookupDN(username);

		if (this.loginCommand.login(dn, password)) {
			this.setFailed(false);
			this.loggedIn = true;
			return "index.xhtml";
		}
		this.setFailed(true);
		return "login.xhtml";
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public boolean isLoggedIn() {
		return this.loggedIn;
	}
	
	public LoginBean() {
	}
}
